package com.toe.commodity.webapi.controller;

import com.toe.commodity.webapi.service.impl.CommodityGoodsServiceImpl;
import com.toe.commodity.webapi.service.impl.CommodityPictureServiceImpl;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.JsonResult;
import com.toe.pojo.entity.commodity.CommodityGoods;
import com.toe.pojo.vo.commodity.AddNewGoods;
import com.toe.pojo.vo.commodity.CommodityGoodsDetailsVO;
import com.toe.pojo.vo.commodity.CommodityGoodsVO;
import com.toe.pojo.vo.commodity.GoodsListDetailsVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/webapi/commodity-goods")
public class CommodityGoodsController {
    @Autowired
    CommodityGoodsServiceImpl commodityGoodsService;

    @Autowired
    CommodityPictureServiceImpl commodityPictureService;

    @PostMapping("/addNewGoodsCommodity")
    @ApiOperation("新增商品")
    public JsonResult addNewCommodity(AddNewGoods addNewGoods, @RequestParam("file") MultipartFile mpfile) {
        //图片上传
        String storeUrlPath = commodityPictureService.pictureUpload(mpfile);
        addNewGoods.setCover(storeUrlPath);
        commodityGoodsService.addNewCommodityGoods(addNewGoods);
        return JsonResult.ok("添加完成");
    }

    @PostMapping("/updateGoodsCommodity")
    @ApiOperation("修改商品信息")
    public JsonResult updateCommodity(CommodityGoods commodityGoods, @RequestParam("file") MultipartFile mpfile) {
        String storeUrlPath = commodityPictureService.pictureUpload(mpfile);
        commodityGoods.setCover(storeUrlPath);
        commodityGoodsService.updateCommodityGoods(commodityGoods);
        return JsonResult.ok("添加完成");
    }

    @GetMapping("/deletedCommodityGoodsById/{id}")
    @ApiOperation("根据商品id删除商品")
    public JsonResult deleteCommodity(@PathVariable("id") Long id) {
        commodityGoodsService.deletedCommodityGoods(id);
        return JsonResult.ok("删除完成");
    }

    @GetMapping("/getGoodsById/{id}/{userId}")
    @ApiOperation("根据商品id查询商品")
    public JsonResult getGoodsById(@PathVariable("id") Long id,@PathVariable("userId") Long userId) {
        CommodityGoodsDetailsVO goodsDetails = commodityGoodsService.setHistoryGoodsById(id, userId);
        return JsonResult.ok("查询成功",goodsDetails);

    }

    @GetMapping("/getCommodityGoodsBySales/{pageNum}/{pageSize}")
    @ApiOperation("根据商品销量查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityBySale(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
        JsonPage<GoodsListDetailsVO> jsonPage = commodityGoodsService.getCommodityGoodsBySales(pageNum, pageSize);
        return JsonResult.ok("查询完成", jsonPage);
    }


    @GetMapping("/getCommodityGoodsByType/{pageNum}/{pageSize}/{id}")
    @ApiOperation("根据商品类型查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityByType(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,@PathVariable("id") Long id) {
        JsonPage<GoodsListDetailsVO> jsonPage = commodityGoodsService.getCommodityGoodsByGoodsTypeId(pageNum, pageSize, id);
        return JsonResult.ok("查询完成", jsonPage);
    }

    @GetMapping("/getCommodityGoodsByBusiness/{pageNum}/{pageSize}/{id}")
    @ApiOperation("根据商家查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityByBusiness(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,@PathVariable("id") Long id) {
        JsonPage<GoodsListDetailsVO> jsonPage =
                commodityGoodsService.getCommodityGoodsByBusinessId(pageNum, pageSize, id);
        return JsonResult.ok("查询完成", jsonPage);
    }
}

